<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:12
 */
namespace App\Ordersys\Models;

use App\Admin\Models\MemberModel;
use App\Base\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class OrdersysOrderModel extends  BaseModel {
    use SoftDeletes;
    /**
     * 表名
     * @var string
     */
    public $table="ordersys_order";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps=true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';

    /**
     * 删除
     */
    const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';

    /**
     * @return WxuserModel
     */
    public function getWxuserModel(){
        return app()->make(WxuserModel::class);
    }

    /**
     * @return MemberModel
     */
    public function getMemberModel(){
      return   app()->make(MemberModel::class);
    }
    /**
     * 时间变成时间刷
     * @return string
     */
    public function setAddTimeAttribute($value){
        $this->attributes['add_time'] =strtotime($value);
        //$this->attributes['today_time'] =strtotime(date("Y-m-d",strtotime($value)));  //取当天的时间截--做了日志表
    }


    /**
     * 时间变成时间刷
     * @return string
     */
    public function setWxNoAttribute($value){
        $this->attributes['wx_no'] =$value;
        $this->attributes['wx_name']=$this->getWxuserModel()->newQuery()->where("wx_no",$value)->value("name");
    }
    /**
     * 销售员
     * @return string
     */
    public function setSaleIdAttribute($value){
        $this->attributes['sale_id'] =$value;
        $this->attributes['sale_name']=$this->getMemberModel()->newQuery()->where("uid",$value)->value("nickname");
    }


    /**
     * 关联快递信息
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public  function express(){
        return $this->hasOne(OrdersysOrderExpressModel::class,"order_id","id");
    }

    /**
     * 关联产品
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public  function goods(){
        return $this->hasMany(OrdersysOrderGoodsModel::class,"order_id","id");
    }


}
